package com.doge.calcalbum.business.album.migrate

import android.os.Bundle
import android.view.*
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.doge.calcalbum.business.album.AlbumEditNameDialogFragment
import com.doge.calcalbum.business.album.AlbumViewModel
import com.doge.calcalbum.core.BaseDialogFragment
import com.doge.calcalbum.core.base.adapters.ITEM_CLICKED
import com.doge.calcalbum.databinding.DialogFragmentAlbumBinding
import com.doge.calcalbum.db.model.Album
import com.doge.calcalbum.utils.setOnClickCallback

class AlbumMigrateDialogFragment : BaseDialogFragment() {
    override val hasShadow = true
    private var innerBinding: DialogFragmentAlbumBinding? = null
    private val binding get() = innerBinding!!
    private val viewModel by viewModels<AlbumViewModel>()
    private val dialogViewModel by activityViewModels<AlbumMigrateDialogViewModel>()
    private val albumType by lazy { arguments?.getInt(EXTRA_ALBUM_TYPE)!! }
    private val adapter by lazy { AlbumMigrateAdapter() }

    override fun setWindow(win: Window) {
        super.setWindow(win)
        val lp = win.attributes
        lp.width = WindowManager.LayoutParams.MATCH_PARENT
        lp.height = WindowManager.LayoutParams.MATCH_PARENT
        win.attributes = lp
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        innerBinding =
            innerBinding ?: DialogFragmentAlbumBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding.outside.setOnClickCallback { dismissAllowingStateLoss() }
        binding.createAlbum.setOnClickCallback {
            AlbumEditNameDialogFragment.newInstance(Album.TYPE_PICTURE).show(childFragmentManager)
        }
        binding.recyclerView.itemAnimator = null
        binding.recyclerView.layoutManager = LinearLayoutManager(binding.recyclerView.context)
        binding.recyclerView.adapter = adapter
        adapter.setListener(::handleAdapterCallback)
        when (albumType) {
            Album.TYPE_PICTURE -> {
                viewModel.pictureAlbumList.observe(viewLifecycleOwner) {
                    adapter.submitList(it)
                }
            }
            Album.TYPE_VIDEO -> {
                viewModel.videoAlbumList.observe(viewLifecycleOwner) {
                    adapter.submitList(it)
                }
            }
        }

    }

    private fun handleAdapterCallback(action: String, item: Any?) {
        when (action) {
            ITEM_CLICKED -> {
                dialogViewModel.select(item as? Album?)
                dismissAllowingStateLoss()
            }
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        binding.createAlbum.setOnClickListener(null)
        binding.recyclerView.adapter = null
        adapter.setListener(null)
        innerBinding = null
    }


    companion object {
        private const val EXTRA_ALBUM_TYPE = "albumType"
        fun newInstance(
            albumType: Int,
        ): AlbumMigrateDialogFragment {
            return AlbumMigrateDialogFragment().apply {
                arguments = Bundle().apply {
                    putInt(EXTRA_ALBUM_TYPE, albumType)
                }
            }
        }
    }

}